package com.qbw.core.base;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import android.os.Bundle;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.fragment.app.DialogFragment;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.qbw.app.R;


/**
 * Created by qinbaowei on 2018/05/17
 */
public abstract class BaseDialogFragment<B extends ViewDataBinding> extends DialogFragment {

    protected B mBinding;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onSetStyle();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, onGetLayoutId(), container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();
        onSetWindowFlag();
    }

    protected void onSetWindowFlag() {
        Window window = getDialog().getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);//去掉系统添加的边距
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = onSetWindowHeight();
        int windowAnim = getWindowAnimations();
        if (windowAnim > 0) {
            window.setWindowAnimations(windowAnim);
        }
        window.setAttributes(params);
        window.setGravity(Gravity.CENTER);
    }

    protected int onSetWindowHeight() {
        return WindowManager.LayoutParams.WRAP_CONTENT;
    }

    protected @StyleRes
    int getWindowAnimations() {
        //return R.style.DialogAnimation;
        return 0;
    }

    protected abstract @LayoutRes
    int onGetLayoutId();

    protected void onSetStyle() {
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.BaseDialog);
    }


}
